package com.ebupt.migu.music.common.util;

import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.system.entity.SysUser;
import com.ebupt.migu.music.system.mapper.UserMapper;
import com.google.common.collect.Maps;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuzz
 */
@Component
@Slf4j
public class ReqestParamUtil {

    @Resource
    private UserMapper userMapper;

    /**
     * 获取request中的key值
     *
     * @param request request
     * @return Map
     */
    public static Map<String, Object> getParams(HttpServletRequest request) {
        Map<String, Object> paramMap = Maps.newHashMapWithExpectedSize(8);
        Enumeration enu = request.getParameterNames();
        //封装查询条件
        while (enu.hasMoreElements()) {
            String paraName = (String) enu.nextElement();
            log.debug(paraName + "=" + request.getParameter(paraName));
            paramMap.put(paraName, request.getParameter(paraName));
        }
        //分页
        if (paramMap.get("current") != null && paramMap.get("every") != null) {
            Integer current = Integer.parseInt(paramMap.get("current").toString());
            Integer every = Integer.parseInt(paramMap.get("every").toString());
            paramMap.put("pageStart", (current - 1) * every);
            paramMap.put("pageEnd", current * every);
            paramMap.put("rows", every);
        }
        return paramMap;
    }

    /**
     * 获取请求参数并映射到Map
     * 添加用户登录信息
     *
     * @param request
     * @return Map
     */
    public static Map<String, Object> getParamsWithUserInfo(HttpServletRequest request) {
        // 获取用户登录信息
        SysUser user = AuthBaseUtils.getSessionUser(request);
        Map<String, Object> paramMap = Maps.newHashMapWithExpectedSize(8);
        Enumeration enu = request.getParameterNames();
        //封装查询条件
        while (enu.hasMoreElements()) {
            String paraName = (String) enu.nextElement();
            log.debug(paraName + "=" + request.getParameter(paraName));
            paramMap.put(paraName, request.getParameter(paraName));
        }

        //分页
        if (paramMap.get("current") != null && paramMap.get("every") != null) {
            Integer current = Integer.parseInt(paramMap.get("current").toString());
            Integer every = Integer.parseInt(paramMap.get("every").toString());
            paramMap.put("pageStart", (current - 1) * every);
            paramMap.put("pageEnd", current * every);
            paramMap.put("rows", every);
        }
        // 添加登录用户身份信息到访问参数
        paramMap.put("cid", user.getId());
        paramMap.put("creatorId", user.getLoginName());
        paramMap.put("creatorName", user.getUserName());
        paramMap.put("creatorDepartmentId", user.getDeptId());
        paramMap.put("creatorDepartment", user.getDeptName());
        paramMap.put("creatorPhone", user.getMobile());
        paramMap.put("creatorEmail", user.getEmail());

        return paramMap;
    }

    /**
     * 获取用户信息
     *
     * @param request
     * @return
     */
    public static SysUser userInfo(HttpServletRequest request) {
        // 获取用户登录信息
        SysUser user = AuthBaseUtils.getSessionUser(request);
        return user;
    }

    /**
     * 获取请求参数并映射到Map
     * 添加用户登录信息
     *
     * @param request
     * @return Map
     */
    public static Map<String, Object> getParamsWithUserInfo(Map paramMap, HttpServletRequest request) {
        // 获取用户登录信息
        SysUser user = AuthBaseUtils.getSessionUser(request);
        /*Map<String, Object> paramMap = Maps.newHashMapWithExpectedSize(8);
        Enumeration enu = request.getParameterNames();
        //封装查询条件
        while (enu.hasMoreElements()) {
            String paraName = (String) enu.nextElement();
            log.debug(paraName + "=" + request.getParameter(paraName));
            paramMap.put(paraName, request.getParameter(paraName));
        }*/

        //分页
        if (paramMap.get("current") != null && paramMap.get("every") != null) {
            Integer current = Integer.parseInt(paramMap.get("current").toString());
            Integer every = Integer.parseInt(paramMap.get("every").toString());
            paramMap.put("pageStart", (current - 1) * every);
            paramMap.put("pageEnd", current * every);
            paramMap.put("rows", every);
        }
        // 添加登录用户身份信息到访问参数
        paramMap.put("cid", user.getId());
        paramMap.put("creatorId", user.getLoginName());
        paramMap.put("creatorName", user.getUserName());
        paramMap.put("creatorDepartmentId", user.getDeptId());
        paramMap.put("creatorDepartment", user.getDeptName());
        paramMap.put("creatorPhone", user.getMobile());
        paramMap.put("creatorEmail", user.getEmail());

        return paramMap;
    }

    /**
     * 获取请求参数并从token中获取用户信息
     *
     * @param request 请求信息
     * @return 参数信息
     */
    public Map<String, Object> getParams2(HttpServletRequest request) {
        String token = request.getHeader("token");
        // 解析token
        Claims claims = TokenUtils.parseJWT(token);

        // 获取loginName
        String loginName = claims.getId();
        log.info("用户登录账号：{}", loginName);
        Map<String, Object> login = new HashMap<>(3);
        login.put("pageStart", 0);
        login.put("rows", 9999);
        login.put("loginName", loginName);
        SysUser user = userMapper.queryByPage(login).get(0);

        Map<String, Object> paramMap = getParams(request);

        // 添加登录用户身份信息到访问参数
        paramMap.put("cid", user.getId());
        paramMap.put("creatorId", user.getLoginName());
        paramMap.put("creatorName", user.getUserName());
        paramMap.put("creatorDepartmentId", user.getDeptId());
        paramMap.put("creatorDepartment", user.getDeptName());
        paramMap.put("creatorPhone", user.getMobile());
        paramMap.put("creatorEmail", user.getEmail());

        return paramMap;
    }
}
